package com.jy.adscm.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyRole;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.dao.mysql.JyRoleSecurityMapper;
import com.jy.adscm.system.mapper.mysql.JyRoleMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.system.service.JyUserService;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统用户表 服务实现类
 * </p>
 *
 * @author yyy
 * @since 2018-08-01
 */
@Service
public class JyUserServiceImpl extends ServiceImpl<JyUserMapper, JyUser> implements JyUserService {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JyRoleSecurityMapper jyRoleSecurityMapper;

    @Override
    public Page<JyUser> getUserPage(Page<JyUser> page) throws AdscmSystemException {
        logger.info("分页查询用户信息");

        List<JyUser> users = jyUserMapper.selectPage(page, null);

        if (users != null && users.size()>0){
            for (JyUser user :
                    users) {
                String photoPath = user.getPhoto();
                if (AdscmBaseUtils.getInstance().isNotEmpty(photoPath,null)){
                    String photo = AdscmBaseUtils.getInstance().readImgToBase64(photoPath);
                    user.setPhoto(photo);
                }

                user.setRoles(jyRoleSecurityMapper.queryUsersRole(user.getId()));
            }
        }
        page.setTotal(jyUserMapper.selectCount(null));
        page.setRecords(users);

        return page;
    }

    @Override
    public void delUser(int userId) throws AdscmSystemException {

        logger.info("删除用户【"+userId+"】信息");

        try{
            JyUser user = jyUserMapper.selectById(userId);
            if (AdscmBaseUtils.getInstance().isNotEmpty(user,"id")){
                user.setEnabled(false);
                jyUserMapper.updateById(user);

            }else {
                throw new AdscmSystemException("没有找到ID为【"+userId+"】的用户信息，不能删除");
            }
        }catch (Exception e){
            logger.info("删除用户【"+userId+"】信息异常");
            throw new AdscmSystemException(e.getMessage());
        }


    }

    @Override
    public void setUserRoles(List<Integer> roleIds, int userId) throws AdscmSystemException {

        logger.info("给用户【"+userId+"】授予角色【"+roleIds+"】");

        try{
            JyUser user = jyUserMapper.selectById(userId);
            if (AdscmBaseUtils.getInstance().isNotEmpty(user,"id")){

                jyUserMapper.delUserAllRole(userId);
                if (roleIds != null && roleIds.size() >0 ){
                    jyUserMapper.setUsersRoles(userId,roleIds);
                }

            }else {
                throw new AdscmSystemException("没有找到ID为【"+userId+"】的用户信息，不能给用户授予角色");
            }
        }catch (Exception e){
            logger.info("给用户【"+userId+"】授予角色【"+roleIds+"】异常");
            throw new AdscmSystemException(e.getMessage());
        }

    }

    @Override
    public List<JyUser> queryUsersByRoleId(String roleId) {
        List<JyUser> jyUsers = jyUserMapper.queryUsersByRoleId(roleId);
        return jyUsers;
    }
}
